/**
 * Created by sky_wang on 2017/4/14.
 */
var REPORT_VIEW.VIEW_DELETED={};
var REPORT_DEL = REPORT_VIEW.VIEW_DELETED;
REPORT_DEL.data = new Array();
REPORT_DEL.view = new Object();
REPORT_DEL.queryAllObj = new Array();
REPORT_DEL.options= {
    columnDefs             : [],
    rowData                : [],
    enableSorting: true,
    rowHeight: 40,
    colWidth: 100,
    showToolPanel:false,
    suppressColumnVirtualisation:true,
    toolPanelSuppressRowGroups:true,
    toolPanelSuppressValues: true,
    toolPanelSuppressPivots: true,
    toolPanelSuppressPivotMode: true

};
REPORT_DEL.refreshData = function (){
    $(".del_show_select_view_options").empty();
    $(".del_show_select_view_options").append("<option value='default'>---请选择---</option>");
    $.get("view/list", function(data) {
        REPORT_DEL.data = data;
        console.log(data);
        for (var i =0; i < data.length; i++)
        {
            if (null == data[i]["view_name"])
            {
                continue;
            }
            $(".del_show_select_view_options").append("<option value='"+data[i]["vid"]+"'>"+data[i]["view_name"]+"</option>");
        }
    });
}
$(document).ready(function() {
    REPORT_DEL.refreshData();
    $('.del_show_content').hide();

    var gridDiv = document.querySelector('.del_show_grid');
    new agGrid.Grid(gridDiv, REPORT_DEL.options);
    $(".del_show_confirm_view").click(function () {
        if ($(".del_show_select_view_options").val() == 'default')
        {
            alert("请选择模版")
            return;
        }
        REPORT_DEL.data.map(function (ele){
            if ($(".del_show_select_view_options").val() == ele["vid"]) {
                REPORT_DEL.view = ele;
                console.log("REPORT_DEL.view:" + REPORT_DEL.view);
                if ("" != REPORT_DEL.view["query_ids"]) {
                    REPORT_DEL.queryAllObj = JSON.parse(REPORT_DEL.view["query_ids"]);
                }
                $(".del_show_query_options").html( REPORT_DEL.view["query_html"]);
                REPORT_DEL.options.api.setColumnDefs(JSON.parse(REPORT_DEL.view["cols_of_sub_groups"]));
                REPORT_DEL.options.api.softRefreshView();
            }
        });

        $('.del_show_content').show();
    });

    $(".del_show_search").click(function() {
        var queryOpt = [];
        for (var i =0; i < REPORT_DEL.queryAllObj.length; i++)
        {
            var query_sub_opt = new Object();
            var query_sub_opt_value = new Object();
            var query_id = "#" +  REPORT_DEL.queryAllObj[i]["key"];
            query_sub_opt_value = REPORT_DEL.queryAllObj[i]["value"];
            if (null == $(query_id).val() || '' == $(query_id).val() || 'default' == $(query_id).val())
            {
                continue;
            }
            query_sub_opt["key"]= REPORT_DEL.queryAllObj[i]["key"];
            query_sub_opt_value["value"] = $(query_id).val();
            query_sub_opt["value"] = query_sub_opt_value;
            queryOpt.push(query_sub_opt);
        }

        $.get("view/"+REPORT_DEL.view["vid"]+"/query_data", {query_data:JSON.stringify(queryOpt)},function(data){
            REPORT_DEL.options.api.setRowData(data);
            //gridOptions.api.refresh();
        });
    });
    $(".del_show_del_view").click(function () {
        if ($(".del_show_select_view_options").val() == 'default')
        {
            alert("请选择模版")
            return;
        }
        console.log("vid="+$(".del_show_select_view_options").val());
        $.ajax({
            type : "delete",
            url  : "view/"+ $(".del_show_select_view_options").val(),
            data : {},
            async : false,
            success : function(){
                REPORT_DEL.refreshData();
                $(".del_show_select_view_options").val('default')
                $('.del_show_content').hide();
                alert("删除成功");
            }
        });

    });


});